<?php
/**
* FileName: admin.seftaghelpdesk.php
* Date: 24/03/2010
* Development Aleksey Pakholkov, Andrey Kvasnevskiy - OrdaSoft(http://ordasoft.com)
* @package SefTranslate
* @copyright 2010 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru);Aleksey Pakholkov
* Homepage: http://www.ordasoft.com
* @version: 1.0.0 free $
* JOS Version #: 1.5.x
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/

// Don't allow direct linking
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

$GLOBALS['option'] = $option = JArrayHelper::getValue($_REQUEST,'option',"com_seftranslate");
$GLOBALS['task'] = $task = JArrayHelper::getValue($_REQUEST,'task',"");

	//print_r("|".$task);exit;
	switch ($task) {
		case 'clear_map':
			clear_map();
			setting();
		     break;
		case 'apply':
			apply();
		
		default:
			setting();
			break;
	}
function clear_map()
{
	if(is_file(JPATH_SITE."/components/com_seftranslate/map.cch"))
	{
		unlink(JPATH_SITE."/components/com_seftranslate/map.cch");
	}

}
function apply()
{
  $settings['site_language']=JArrayHelper::getValue($_REQUEST,'site_language',"en");
  $settings['map_lang']=JArrayHelper::getValue($_REQUEST,'map_lang',"en");
  $settings['cache_map']=JArrayHelper::getValue($_REQUEST,'cache_map',"0");
  $settings['translator']=JArrayHelper::getValue($_REQUEST,'translator',"bing");
  $settings['api_translate']=JArrayHelper::getValue($_REQUEST,'api_translate',"");
  $settings['appId']=JArrayHelper::getValue($_REQUEST,'appId',"");
	setParams($settings);
}
function setParams($settings)
	{
    $set = "<?php\n";
    $set .= "// Do not edit this file. Generated by admin script.\n";
    $set .= "// Translate Configuration file\n";
    $set .= "// General Informations \n";
    $set .= "\$settings['site_language']='" . $settings['site_language'] ."';\n";
    $set .= "\$settings['map_lang']='" . implode("|",$settings['map_lang']) ."';\n";
    $set .= "\$settings['cache_map']='" . $settings['cache_map']."';\n";
    $set .= "\$settings['translator']='" . $settings['translator']."';\n";
    $set .= "\$settings['api_translate']='" . $settings['api_translate']."';\n"; 
    $set .= "\$settings['appId']='" . $settings['appId']."';\n";         
	
		$fd = fopen(JPATH_SITE."/components/com_seftranslate/languages.conf.php", "w" );
		fwrite( $fd, $set );
		fclose( $fd );
	}
function setting()
{

		$settings=Array();
		{
			require_once(JPATH_SITE."/components/com_seftranslate/languages.conf.php");
		}
		//print_r($settings);
?>
<form action="index.php" method="post" name="adminForm" >
<?php
	jimport('joomla.html.pane');
	//Get JPaneTabs instance
	$myTabs = & JPane::getInstance('tabs', array('startOffset'=>0));
	$output = '';
	//Create Pane
	echo $myTabs->startPane( 'pane' );
	//Create 1st Tab
	echo $myTabs->startPanel( 'General', 'tab1' );
	settingGeneral($settings);
	echo $myTabs->endPanel();
	
	//Create 3rd Tab
	echo $myTabs->startPanel('Map', 'tab3' );
	settingsMap($settings);
	echo $myTabs->endPanel();

	echo $myTabs->startPanel('About', 'tab4' );
	about($settings);
	echo $myTabs->endPanel();

	echo $myTabs->startPanel('Help', 'tab5' );
	help($settings);
	echo $myTabs->endPanel();

	//End Pane
	echo $myTabs->endPane();

?>

	<input name="task" id="task" value="apply" type="hidden">
	<input name="option" id="option" value="com_seftranslate" type="hidden">	
	</form>	
<?php
}
function about($settings)
{
	echo "<div>";
	if(is_file(JPATH_SITE."/administrator/components/com_seftranslate/doc/about.html"))
	{
		$fp = fopen(JPATH_SITE."/administrator/components/com_seftranslate/doc/about.html", "r"); 
		$contents = fread($fp, filesize(JPATH_SITE."/administrator/components/com_seftranslate/doc/about.html"));
		fclose($fp);
		echo $contents;
		
	}
	else
	{
		echo "Sorry! File not found.";
	}
	echo "</div>";

}
function help($settings)
{
	echo "<div>";
	if(is_file(JPATH_SITE."/administrator/components/com_seftranslate/doc/help.html"))
	{
		$fp = fopen(JPATH_SITE."/administrator/components/com_seftranslate/doc/help.html", "r"); 
		$contents = fread($fp, filesize(JPATH_SITE."/administrator/components/com_seftranslate/doc/help.html"));
		fclose($fp);
		echo $contents;
		
	}
	else
	{
		echo "Sorry! File not found.";
	}
	echo "</div>";


}
function settingGeneral($settings)
{
		$lang_list=parse_ini_file(JPATH_SITE."/components/com_seftranslate/languages.ini");
		$keys = array_keys( $lang_list );
		// iterate through styles
		$langs=Array();
		foreach( $keys as $key )
		{
			if($key!="UNKNOWN")
			{
				$t=new t_langs();
				$t->key=$lang_list[$key];
				$t->title=ucfirst(strtolower($key));
				$langs[]=$t;
			}
		}
  //just simly copy as previous    
$translate_list=Array();
$translate_list['bing']='bing';
$translate_list['gtranslate']='gtranslate'; 
$keys=array_keys($translate_list);
$trans=Array();
foreach($keys as $key){
  if($key!="UKNOWN"){
    $t=new t_langs;
    $t->key=$translate_list[$key];
    $t->title=ucfirst(strtolower($key));
    $trans[]=$t;
    }
  }  
  
	?>
	<table>
		<tr>
			<td>Site Language:</td>
			<td>
				<?php
				echo JHTML::_('select.genericlist',  $langs, 'site_language', '', 'key', 'title', $settings['site_language'], 'site_language' );
				?>
			</td>
			<td>
				<?php echo JHTML::_('tooltip', "The main language of your site." );?>
			</td>
		<tr>
		    <tr>
      <td>Use bing or Google API for translate:</td>
      <td>
        <?php
        echo JHTML::_('select.genericlist',  $trans, 'translator', '', 'key', 'title', $settings['translator'], 'translator' );
        ?>
      </td>
      <td>
        <?php echo JHTML::_('tooltip', "The main language of your site." );?>
      </td>
    <tr>
        <tr>
      <td>Key Google API Translate:</td>
      <td>
       <input type="text" name="api_translate" value="<?php if(isset($settings['api_translate'])) echo $settings['api_translate'];?>" class="inputbox"/>
      </td>
      <td>
        <?php echo JHTML::_('tooltip', "The main language of your site." );?>
      </td>
    <tr>
        <tr>
        <tr>
      <td>Key Bing API Translate:</td>
      <td>
       <input type="text" name="appId" value="<?php if(isset($settings['appId'])) echo $settings['appId'];?>" class="inputbox"/>
      </td>
      <td>
        <?php echo JHTML::_('tooltip', "The main language of your site." );?>
      </td>
    <tr>
	</table>


	<?php

}
function settingsMap($settings)
{
	//print_r(explode("|",$settings['map_lang']));exit;
	$lang_list=parse_ini_file(JPATH_SITE."/components/com_seftranslate/languages.ini");
	$keys = array_keys( $lang_list );
		// iterate through styles
		$langs=Array();
		foreach( $keys as $key )
		{
			if($key!="UNKNOWN")
			{
				$t=new t_langs();
				$t->key=$lang_list[$key];
				$t->title=ucfirst(strtolower($key));
				$langs[]=$t;
			}
		}
	?>
	<table>
		
		<tr>
			<td>Translate page on Site Map:</td>
			<td>
				<?php
				echo JHTML::_('select.genericlist',  $langs, 'map_lang[]', 'class="inputbox" multiple size="8"', 'key', 'title', explode("|",$settings['map_lang']), 'map_lang');
				?>
			</td>
			<td>
				<?php echo JHTML::_('tooltip', "Set language when need translate site map." );?>
			</td>
		</tr>
		<tr>
			<td>Use cache for Site Map:</td>
			<td>
				<input type="radio" name="cache_map" id="log1" value="0"
				<?php echo ($settings['cache_map'] == 0) ? " checked=\"checked\"" : " "; ?> />
				<label for="log1" style="clear:none">No</label>
				<input type="radio" name="cache_map" id="log2" value="1"
				<?php echo ($settings['cache_map'] == 1) ? " checked=\"checked\"" : " "; ?> />
				<label for="log2" style="clear:none">Yes</label> 
			</td>
			<td>
				<?php echo JHTML::_('tooltip', "Set language when need translate site map." );?>
			</td>
		</tr>
		<tr>
			<td>Clear cache Site map:</td>
			<td>
				
				<?php
				if(is_file(JPATH_SITE."/components/com_seftranslate/map.cch"))
				{
				?>
				<input type="submit" value="Clear..." id="log" onClick="adminForm.task.value='clear_map'">
				<?php }else{ ?>
					Cache is clear.
				<?php } ?>
			</td>
			<td>
				<?php echo JHTML::_('tooltip', "Clear you cache for site map." );?>
			</td>
		</tr>
	</table>
	<?php
	

}
function settingCache()
{

}


class t_langs
{
	var $key;
	var $title;
}		
		
?> 
